package com.campus.wall.ui;

import android.app.PendingIntent;
import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.Ndef;
import android.os.Parcelable;

import com.campus.wall.R;
import com.campus.wall.base.BaseMvpActivity;
import com.campus.wall.base.NfcUtils;
import com.campus.wall.contract.NftContract;
import com.campus.wall.presenter.NftPresenter;
import com.hjq.toast.ToastUtils;

import java.io.UnsupportedEncodingException;

import butterknife.OnClick;

public class NftActivity extends BaseMvpActivity<NftPresenter> implements NftContract.View {


    @Override
    public int getLayoutId() {
        return R.layout.layout_nft;
    }

    @OnClick(R.id.btn_back)
    void btn_back(){
        this.finish();
    }

    @Override
    public void initView() {

        NfcUtils nfcUtils = new NfcUtils(this);
    }


    @Override
    protected void onResume() {
        super.onResume();
        //开启前台调度系统
        NfcUtils.mNfcAdapter.enableForegroundDispatch(this, NfcUtils.mPendingIntent, NfcUtils.mIntentFilter, NfcUtils.mTechList);
    }
    @Override
    protected void onPause() {
        super.onPause();
        //关闭前台调度系统
        NfcUtils.mNfcAdapter.disableForegroundDispatch(this);
    }
    @Override
    public void onNewIntent(Intent intent) {
        //1.获取Tag对象
        super.onNewIntent(intent);
        try {
            String str = NfcUtils.readNFCFromTag(intent);
             intent = new Intent(this, PlaceActivity.class);
            startActivity(intent);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    private void readNfcTag(Intent intent) {
        if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
            Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
                    NfcAdapter.EXTRA_NDEF_MESSAGES);
            NdefMessage msgs[] = null;
            int contentSize = 0;
            if (rawMsgs != null) {
                msgs = new NdefMessage[rawMsgs.length];
                for (int i = 0; i < rawMsgs.length; i++) {
                    msgs[i] = (NdefMessage) rawMsgs[i];
                    contentSize += msgs[i].toByteArray().length;
                }
            }
            try {
                if (msgs != null) {
                    NdefRecord record = msgs[0].getRecords()[0];
                    ToastUtils.show("读取成功");
                }
            } catch (Exception e) {
            }
        }
    }
    @Override
    public void onSuccess() {

    }


}
